package com.aerozhonghuan.oknet2;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * 构建 json字符串
 * Created by zhangyunfei on 17/6/23.
 */

public class JsonBodyBuilder {
    private HashMap<String, String> data;

    public JsonBodyBuilder() {
        data = new HashMap<>();
    }

    public JsonBodyBuilder put(String key, String value) {
        data.put(key, value);
        return this;
    }

    public int size() {
        return data.size();
    }

    public String build() {
        JSONObject jsonObject = new JSONObject();

        for (Map.Entry<String, String> $i : data.entrySet()) {
            try {
                jsonObject.put($i.getKey(), $i.getValue());
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
        return jsonObject.toString();
    }
}
